<?php
/**
 * 会员接口
 * @author maniac
 */

namespace app\api\controller\v1;

use app\common\model\MemberBalance;
use app\common\model\MemberYongjin;
use app\common\model\Orders;
use think\console\input\Option;
use think\Db;
use app\facade\Members AS MembersFacade;
use app\facade\Wechat AS WechatFacade;
use app\facade\Orders as OrdersFacade;
use app\facade\Options;
use app\facade\Aes;
use think\facade\Env;
use app\common\model\MemberRecharge;
use app\common\model\Verify as VerifyModel;
use app\common\model\Members as MembersModel;
use app\common\model\WechatUsers;
use app\common\model\MemberCash AS MemberCashModel;
use app\common\model\Vars AS VarsModel;


class Members extends Base
{
    /*
     *判断token失效重新登录
     * @author 董伟
     * */
    public function user_reset()
    {
        $userToken = input('post.userToken/s', '');
        if (!$userToken) {
            return j(1, '参数为空');
        }
        parse_str(Aes::decrypt($userToken), $data);
        if (!isset($data['id'])) {
            return j(1, '请重新登录');
        } else {

            $info = MembersModel::field(true)->where('id', $data['id'])->find();
            return j(0, '获取会员信息成功', $info);
        }
    }

    /*
     * 站点是否停止
     * @author 董伟
     * */
    public function set_web()
    {
        $config = unserialize(Options::option('app_info'));
        return j(0, '成功', $config['is_web']);
    }

        /**
     * 绑定手机号加修改手机号
     * @author 董伟
     */
    public function bindMobile(){

        $userId = $this->checkUserToken();
        $mobile = input('post.mobile');
        $verifys = input('post.verify');
        $parent_id = input('post.parent_id',0);
        $openid   =input('post.openid',0);

        try {
            $data=[
                'userid'=>$userId,
                'parent_id'=>$parent_id,
                'mobile'=>$mobile,
                'verify'=>$verifys,
                'openid'=>$openid
            ];
            $res=MembersFacade::bindMobile($data);
            return j(0, '绑定成功',$res);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }




    /**
     * 会员会员信息
     */
    public function info()
    {
        $userId = $this->checkUserToken();
        try {
            $info = MembersFacade::info($userId);
            return j(0, '获取会员信息成功', $info);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }

    /*
     * 修改资料
     * @author 董伟
     * */
    public function editInfo()
    {
        $userId = $this->checkUserToken();
        $post = input('param.');
        $info = MembersFacade::editInfo($post, $userId);
        return j(0, '获取会员信息成功', $info);
    }

    /*
     * 我的余额变动
     * @author 董伟
     * */
    public function balanceRecords()
    {
        $userId = $this->checkUserToken();
        $type = input('type', 0);//0全部1增加余额2使用余额
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;
        try {
            $result = MembersFacade::balance($userId, $limit,$type);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }


    /**
     * 积分变动记录
     */
    public function pointsRecords()
    {
        $userId = $this->checkUserToken();
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;
        $type = input('type',0);
        try {
            $result = MembersFacade::pointsRecords($userId, $limit,$type);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }


    /*
     * 充值列表
     * @author 董伟
     * */
    public function recharge_config()
    {
        $result = MembersFacade::rechargeConfig();
        return j(0, '获取数据成功', $result);
    }


    /*
     * 生成充值订单
     * @author 董伟
     * */
    public function submitRecharge()
    {
        $userId = $this->checkUserToken();
        $id = input("id", 0);//列表id
        $type = input('type', 0);//1余额，2积分充值
        $pay_type = input('pay_type', 1);//1微信，支付宝
        $amount = input('amount', 0);

        if (!$amount) {
            return j(1, '金额不合法');
        }

        $arg = array(
            'cz_uid' => $userId,
            'cz_amount' => $amount,
            'cz_points' => 0,
            'cz_type' => $type,
            'pay_type' => $pay_type,
        );
        $ret = MembersFacade::createRechargeOrder($arg);
        if ($ret) {
            return j(0, '成功', $ret['id']);
        } else {
            return j(1, '失败');
        }
    }

    /**
     * 积分变动记录
     */
    public function vouchersRecords()
    {
        $userId = $this->checkUserToken();
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;
        try {
            $result = MembersFacade::vouchersRecords($userId, $limit);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }


    /*
     * 我的销售订单(我的+分销订单)
     * @author 董伟
     * */
    public function myOrder_list()
    {
        $userId = $this->checkUserToken();
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 10);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;

        $lists = MembersModel::field(true)->select()->toArray();
        $ids = MembersFacade::getSubs($lists, $userId, 1);
        $ids = implode(',', $ids);
        try {
            $data = OrdersFacade::orderList($ids, $limit, 0, 0);
            return j(0, '获取订单信息成功', $data);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }

    /*
     * 我的奖励
     * @author 董伟
     * */
    public function my_yongjin()
    {
        $userId = $this->checkUserToken();
        $where[] = [
            ['order_status', 'eq', 1],
            ['pay_status', 'eq', 1],
            ['shipping_status', 'eq', 1],
            ['userid', 'eq', $userId]
        ];

        $data['sum'] = MemberYongjin::field('balance')->where('userid', $userId)->sum('balance');//总奖励的金额
        $data['tixian'] = \app\common\model\Members::field('money')->where('id', $userId)->find();//可提现的金额
        $data['yi_tixian'] = MemberBalance::field(true)->where('userid', $userId)->where('type', 1)->sum('balance');//已提现的金额
        $data['wei_jiesuan'] = Orders::field(true)->where($where)->sum('pay_amount');//未结算的金额

        $data['list'] = MemberYongjin::field(true)->where('userid', $userId)->select()->toArray();
        $data['wei_list'] = Orders::field(true)->where($where)->select()->toArray();

        return j(0, '获取信息成功', $data);
    }

    /*测试*/
    public function test()
    {
        $data = WechatFacade::fx(43);
        return j(0, '获取信息成功', $data);
    }

    /**
     * 用户消息列表
     */
    public function messagelist()
    {
        $userId = $this->checkUserToken();
        $page = input('post.page/d', 1);
        $limit = input('post.limit/d', 20);
        // 分页偏移量
        $offset = ($page - 1) * $limit;
        $limit = $offset . ',' . $limit;
        try {
            $result = MembersFacade::messagelist($userId, $limit);
            return j(0, '获取数据成功', $result);
        } catch (\Exception $e) {
            return j(1, $e->getMessage());
        }
    }

    /**
     * 积分规则
     */
    public function jifen_content()
    {
        $id = input('id',0);
        $data = VarsModel::field('value')->where('id',$id)->find();
        return j(0, '获取数据成功', $data);
    }
	/*
		 * 添加收藏
		 *
		 */
	public function add_collection(){
		$userId = $this->checkUserToken();
		$post = input('post.','');//收藏信息
		try {
			$result = MembersFacade::add_collection($userId,$post);
			if($result){
				return j(0, '成功', $result);
			}
			return j(1, '失败');
		} catch (\Exception $e) {
			return j(1, $e->getMessage());
		}
	}
	/*
	 * 我的收藏
	 * */
	public function collection_list(){
		$userId = $this->checkUserToken();
		$type = input('type','product');//
		$page = input('page',0);
		$limit = input('limit',0);
		try {
			$result = MembersFacade::collection_list($userId,$type,$page,$limit);
			return j(0, '成功', $result);
		} catch (\Exception $e) {
			return j(1, $e->getMessage());
		}
	}
	/**
	 * 删除收藏的商品
	 */
	public function deleteCollect()
	{
		$userId = $this->checkUserToken();
		$id     = input('post.id', 0);
		if (!$id) {
			return j(1, '请选择要移除的商品');
		}
		try {
			MembersFacade::deleteCollect($id);
			return j(0, '移除成功');
		} catch (\Exception $e) {
			return j(1, $e->getMessage());
		}
	}


}
